const axios = require('axios');
const path = require('path');
class Brand {
    constructor(
        name,
        id,
        url,
        alpha
    ) {
        this.name = name
        this.id = id
        this.url = url
        this.alpha = alpha
    }
}
const url = 'https://sapi.16888.com/wxapp/wx4cface15c95f3a46/app.php';
const params = {
    mod: 'api',
    extra: 'ajaxCondition',
    type: 'allBrand'
};
const headers = {
    Host: 'sapi.16888.com',
    Connection: 'keep-alive',
    'xweb_xhr': 1,
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/6.8.0(0x16080000) NetType/WIFI MiniProgramEnv/Mac MacWechat/WMPF MacWechat/3.8.8(0x13080813) XWEB/1227',
    Accept: '*/*',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    Referer: 'https://servicewechat.com/wx4cface15c95f3a46/6/page-frame.html',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Content-Type': 'application/json'
};
const res = []
axios.get(url, {
    params,
    headers,
})
    .then(response => {
        // data.alphaList[].list[].forEach(item => {})
        const data = response.data.data;
        data.alphaList.forEach(item => {
            item.list.forEach(brand => {
                const brandModel = new Brand(brand.title, brand.cat_id, '', brand.alpha);
                res.push(brandModel);
                const json = { data: res };
                // 保存到当前目录/car.json中
                const fs = require('fs');
                fs.writeFileSync(path.join(__dirname, 'brand.json'), JSON.stringify(json, null, 2));
                console.log(brandModel.toString());
            })
        })

    })
    .catch(error => {
        console.error(error);
    });
